package com.dosh;

import android.app.Activity;
import android.database.Cursor;
/**
 * The Category class describes an entry in the Category table.
 * A User can create Categories to organize his Bookings. 
 * The fields category_ID, User_ID and name are necessary for are table entry.
 */
public class Category extends Activity{
	/**
	 * The ID of the Category entry
	 */
	public long category_ID;
	/**
	 * The Category belongs to this User
	 */
	public long User_ID;
	/**
	 * The Categories name
	 */
	public String name;
	
	/**
	 * This Constructor is used, if there is no entry in the database.
	 * @param dba A reference to the DBAdapter
	 */
	public Category(String name, long User_ID, DBAdapter dba) {
		
		this.User_ID = User_ID;
		this.name = name;
		
		dba.open();
		this.category_ID = dba.insertCategory(name, User_ID);
		dba.close();
	}
	/**
	 * This Constructor is used, if there an entry already exists in the database.
	 * @param dba A reference to the DBAdapter
	 */
	public Category(long category_ID, DBAdapter dba) {
		
		this.category_ID = category_ID;
		dba.open();
		Cursor cCursor = dba.SELECTbyId("Category", category_ID);
		dba.close();
		this.name = cCursor.getString(1);
	}
	/**
	 * This method updates the table entry
	 * @param dba A reference to the DBAdapter
	 * @return gives information,if the update was successful
	 */
	public boolean update(DBAdapter dba) 
	{
		boolean bool = true;
		
		dba.open();
		bool = dba.update("Category", this.category_ID, "User_ID", this.User_ID);
		bool = dba.update("Category", this.category_ID, "Name", this.name);
		dba.close();
		
		return bool;
	}
	
}
